package main

import (
	"example/idldata"
	serviceproxy "example/service/proxy"
	dynamicproxy "example/servicedynamic/proxy"
	"fmt"
	"rpc-repo/gomock/utiltool"
	"runtime/debug"
	"time"
)

type MockCaller struct {
	RunTime       int64
	CallCount     int32
	goCount       int32                 //控制总协程数目
	Connector     *utiltool.MockConnect //连接器
	Dura          time.Time
	Serviceclient *serviceproxy.ServiceProxy
	Dynamicclient *dynamicproxy.ServiceDynamicProxy
}

var (
	dummy *idldata.Dummy
	data  *idldata.Data
)

func init() {
	dummy = &idldata.Dummy{
		Field1: 1,
		Field2: "stupid",
		Field3: []string{"stupid1", "stupid2"},
		Field4: make(map[string]bool),
		Field5: make(map[int64]string),
		Field6: true,
		Field7: 2.333,
		Field8: 2.333,
	}

	dummy.Field4["stupid"] = true
	dummy.Field4["foo"] = false
	dummy.Field5[1] = "foo"
	dummy.Field5[2] = "stupid"

	data = &idldata.Data{
		Field1:  2,
		Field2:  "foo",
		Field3:  []string{"foo1", "foo2"},
		Field6:  true,
		Field7:  2.333,
		Field8:  2.333,
		Field9:  dummy,
		Field10: []*idldata.Dummy{dummy},
		Field12: make(map[int64]*idldata.Dummy),
	}

	data.Field12[1] = dummy
}

func dealError(err error) {
	if err != nil {
		fmt.Println(err)
	}
}

func (caller *MockCaller) DoTest() {
	if caller == nil {
		return
	}

	defer func() {
		if r := recover(); r != nil {
			fmt.Printf("[MockCaller] DoTest error %v \n %s \n", r, string(debug.Stack()))
		}
	}()

	if caller.Dynamicclient == nil {
		return
	}

	if caller.Serviceclient == nil {
		return
	}

	var err error

	err = caller.Serviceclient.Method1(data, "foo")
	dealError(err)

	ret1, err := caller.Serviceclient.Method2(1, data.Field4, 2)
	if len(ret1) > 0 {
		fmt.Println(ret1)
	}
	dealError(err)

	ret2, err := caller.Serviceclient.Method3(1, data.Field4, data.Field5, 2)
	if ret2 != nil {
		fmt.Println(ret2)
	}
	dealError(err)

	err = caller.Dynamicclient.Method1(data, "")
	dealError(err)

	_, err = caller.Dynamicclient.Method2(0, data.Field4, 0)
	dealError(err)

	caller.CallCount += 5
	if caller.CallCount%10000 == 0 {
		fmt.Printf("[idlrpc] rpc call count 10000 in %v \n", time.Since(caller.Dura))
		caller.Dura = time.Now()
		caller.CallCount -= 10000
	}
}
